<!--  -->
<template>
  <div class="shopcar">
      <div class="shop-car-left">
          <!-- 购物车logo -->
          <div class="logo-wrap">
            <div class="logo" :class="{'highlight':$store.getters.TotalCount>0}" @click="GoshopCarDetail">
              <span class="icon-shopping_cart" :class="{'highlight':$store.getters.TotalCount>0}"></span>
            </div>
            <span class="badge" v-show="$store.getters.TotalCount>0">{{ $store.getters.TotalCount||0 }}</span>
          </div>
          <div class="price" :class="{'highlight':$store.getters.TotalPrice>0}">
            ￥{{ $store.getters.TotalPrice||0 }}
          </div>
          <div class="desc">配送费￥{{ deliveryPrice }}元</div>
      </div>
      <div class="shop-car-right" :class="payClass">{{ payDesc }}</div>
      <div class="ball"></div>
      <transition name="shopCarDetail">
        <div v-show="popupflag" class="shopCarDetail">
          <div class="title-wrap border-1px">
            <span class="title">购物车</span>
            <span class="clearAll" @click="clearAll">清空</span>
          </div>
          <div class="list-content">
            <ul>
              <li class="food-item border-1px" v-for="food in $store.state.ShopCar" :key="food.id">
                <span class="food-name">{{ food.name }}</span>
                <div class="price-wrap">
                  <span class="food-price"><span>￥</span>{{ food.price*food.count }}</span>
                  <cartcontrol :food="food"></cartcontrol>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </transition>
      <transition name="wrap">
        <div v-show="popupflag" class="wrap" @click="GoshopCarDetail"></div>
      </transition>
  </div>
</template>
<script>
import cartcontrol from "../cartcontrol/cartcontrol.vue";
import BScroll from "better-scroll";
export default {
  data() {
    return {
      // popupflag控制商品详情页面的展开
      popupflag: false
    };
  },
  methods: {
    // 打开购物详情浮动栏
    GoshopCarDetail() {
      // 展开状态随时可以关闭
      if (this.popupflag === true) {
        this.popupflag = false;
        return;
      }
      // 商品数大于0才可以展开
      if (this.$store.getters.TotalCount > 0) {
        this.popupflag = true;
      }
      // 购物车详情滚动效果
      if (this.popupflag) {
        if (!this.shopCarScroll) {
          this.$nextTick(() => {
            this.shopCarScroll = new BScroll(".list-content", {
              click: true
            });
          });
        } else {
          this.$nextTick(() => {
            this.shopCarScroll.refresh();
          });
        }
      }
    },
    clearAll() {
      this.$store.commit("clearAll");
    }
  },
  components: {
    cartcontrol
  },
  computed: {
    // 判断是否达到配送最低额度
    payDesc() {
      if (this.$store.getters.TotalPrice === 0) {
        return `￥${this.minPrice}元起送`;
      } else if (this.$store.getters.TotalPrice < this.minPrice) {
        let diff = this.minPrice - this.$store.getters.TotalPrice;
        return `还差￥${diff}元起送`;
      } else {
        return `去结算`;
      }
    },
    // 计算结算处是否高亮
    payClass() {
      if (this.$store.getters.TotalPrice < this.minPrice) {
        return "not-enough";
      } else {
        return "enough";
      }
    },
    // 将this.$store.getters.TotalCount给计算属性listenTotalCount
    // 在用watch去监听，变相的实现监听vuex内部数据变化
    listenTotalCount() {
      return this.$store.getters.TotalCount;
    }
  },
  watch: {
    // 监听listenTotalCount的改变，当清空购物车后，shopCarDetail自动隐藏
    listenTotalCount: function(newval) {
      if (newval === 0) {
        this.popupflag = false;
      }
    }
  },
  props: ["minPrice", "deliveryPrice"]
};
</script>
<style lang='less' scoped>
@import "../../common/css/common.less";
.shopCarDetail-enter,
.shopCarDetail-leave-to {
  opacity: 0;
  transform: translateY(100%);
}
.wrap-enter,
.wrap-leave-to {
  opacity: 0;
}
.shopcar {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 46px;
  font-size: 0;
  z-index: 200;
  display: flex;
  .shop-car-left {
    flex: 1;
    background-color: #141d27;
    z-index: 200;
    .logo-wrap {
      display: inline-block;
      position: relative;
      top: -10px;
      margin: 0 12px 0 12px;
      padding: 6px;
      height: 56px;
      width: 56px;
      box-sizing: border-box;
      background-color: #141d27;
      border-top-right-radius: 50%;
      border-top-left-radius: 50%;
      .logo {
        width: 100%;
        height: 100%;
        background-color: #2b343c;
        border-radius: 50%;
        text-align: center;
        &.highlight {
          background-color: rgb(0, 160, 240);
        }
        .icon-shopping_cart {
          &.highlight {
            color: white;
          }
          .font-style(24px,44px);
          color: rgba(255, 255, 255, 0.4);
        }
      }
      .badge {
        position: absolute;
        top: 0;
        right: 0;
        width: 24px;
        height: 16px;
        .font-style(9px,16px,700);
        text-align: center;
        border-radius: 16px;
        color: white;
        background-color: rgb(240, 20, 20);
        box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.4);
      }
    }
    .price {
      display: inline-block;
      vertical-align: top;
      margin-top: 10px;
      padding-right: 12px;
      border-right: 1px solid rgba(255, 255, 255, 0.1);
      .font-style(16px, 24px, 700);
      color: rgba(255, 255, 255, 0.4);
      &.highlight {
        color: rgb(255, 255, 255);
      }
    }
    .desc {
      display: inline-block;
      vertical-align: top;
      margin: 10px 0 0 12px;
      .font-style(10px,24px);
      color: rgba(255, 255, 255, 0.4);
    }
  }
  .shop-car-right {
    flex: 0 0 105px;
    width: 105px;
    .font-style(12px,46px,700);
    text-align: center;
    z-index: 200;
    color: rgba(255, 255, 255, 0.4);
    &.not-enough {
      background-color: #2b343b;
    }
    &.enough {
      background-color: #00b43c;
      color: #fff;
    }
  }
  .shopCarDetail {
    width: 100%;
    position: absolute;
    max-height: 305px;
    bottom: 46px;
    z-index: 100;
    transition: all 0.3s;
    .title-wrap {
      display: flex;
      justify-content: space-between;
      padding: 0 18px;
      height: 40px;
      .border-1px(rgba(7,17,27,0.1));
      background-color: #f3f5f7;
      .title {
        .font-style(14px,40px,200);
        color: rgb(7, 17, 27);
      }
      .clearAll {
        .font-style(12px,40px);
        color: rgb(0, 160, 220);
      }
    }
    .list-content {
      width: 100%;
      max-height: 219px;
      background-color: white;
      overflow: hidden;
      .food-item {
        margin: 0 18px;
        height: 48px;
        display: flex;
        justify-content: space-between;
        .border-1px(rgba(7,17,27,0.1));
        .food-name {
          .font-style(14px,48px);
          color: rgb(7, 17, 27);
        }
        .price-wrap {
          .food-price {
            margin-right: 12px;
            .font-style(14px,48px,700);
            color: rgb(240, 20, 20);
            span {
              font-size: 10px;
            }
          }
          .cartcontrol {
            display: inline-block;
            vertical-align: top;
            margin-top: 12px;
          }
        }
      }
    }
  }
  .wrap {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    background: rgba(7, 17, 27, 0.6);
    transition: all 1s;
  }
}
</style>